<?php

namespace App\Http\Controllers\Admin;

use App\Http\Config\ErrorCode;
use App\Models\User\UserBase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OfficeArticlesController extends BaseController
{
    public function __construct(Request $request){
        parent::__construct($request);
        try {
            if (empty(DB::select('SHOW TABLES LIKE "office_articles"'))) {
                DB::statement("CREATE TABLE `office_articles` (
   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `mall_id` int(10) unsigned DEFAULT 0 COMMENT '所属商城ID',
   `user_id` int(50) DEFAULT 0 COMMENT '用户ID',
   `name` varchar(50) NOT NULL COMMENT '名称',
   `picture` varchar(100) DEFAULT 0 COMMENT '图片',
   `num` int(10) DEFAULT 0 COMMENT '数量',
   PRIMARY KEY (`id`),
   KEY (`user_id`),
   KEY (`mall_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COMMENT='办公物品'");
            }
        }catch (\Exception $e){}
    }

    public function getDataList(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $id = $request->post("id", '');
        $res = DB::table('office_articles as a')
            ->select('au.name as userName','a.*')
            ->leftJoin('admin_users as au','au.id','=','a.user_id');
        if($id!=''){$res->where('a.user_id',$id);}
        if($this->AU["id"] != '1' && $this->AU["mall_id"] != '0'){
            $tt = DB::table('admin_permissions as p')
                ->leftJoin('admin_permission_role as pr','pr.permission_id','=','p.id')
                ->leftJoin('admin_role_user as ru','ru.role_id','=','pr.role_id')
                ->where("ru.user_id", $this->AU["id"])
                ->where("p.name", "admin.hr.allOfficeArticles")
                ->count();
            if(!$tt) {
                $adminUserModel = new UserBase();
                $user_list = $adminUserModel->getAdminSubuser($this->AU["id"]);
                $res->whereIn('a.user_id', $user_list);
            }
        }
        $data = json_decode(json_encode($res->where("a.mall_id", $request->mallData["id"])->get()),true);
        foreach ($data as &$v){
            $v['picture'] = $this->processingPictures($v['picture']);
        }
        $this->returnData['data'] = $data;
        return $this->return_result($this->returnData);
    }

    public function update(Request $request,$id){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $res = DB::table('office_articles')->where("id", $id)->update([
            "name" => $request->post("name", ''),
            "picture" => $request->post("picture", ''),
            "num" => $request->post("num", 1),
        ]);
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = "更新资料失败";
            return $this->return_result($this->returnData);
        }
        $this->returnData['msg'] = "更新资料成功";
        return $this->return_result($this->returnData);
    }

    /* 暂不用，后面再处理 */
    public function insert(Request $request){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $res = DB::table('office_articles')->insert([
            "user_id" => $request->post("user_id", $this->AU["id"]),
            "name" => $request->post("name", ''),
            "picture" => $request->post("picture", ''),
            "num" => $request->post("num", 1),
            "mall_id" => $request->mallData["id"]
        ]);
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = "插入资料失败";
            return $this->return_result($this->returnData);
        }
        $this->returnData['msg'] = "插入资料成功";
        return $this->return_result($this->returnData);
    }

    public function del($id){
        if ($this->returnData['code'] > 0){
            return $this->returnData;
        }
        $res = DB::table('office_articles')->delete([
            "id" => $id
        ]);
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = "删除资料失败";
            return $this->return_result($this->returnData);
        }
        $this->returnData['msg'] = "删除资料成功";
        return $this->return_result($this->returnData);
    }
}
